package com.tuanzi.loan.business.repository.galaxycard.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardPrivilege.LOAN_GALAXY_CARD_PRIVILEGE;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardPrivilegeStatus.LOAN_GALAXY_CARD_PRIVILEGE_STATUS;

import java.util.Map;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.galaxycard.GalaxyCardPrivilegeStatusRepository;
import com.tuanzi.loan.business.service.galaxycard.impl.GalaxyCardPrivilegeServiceImpl;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardPrivilegeStatusSearchRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardPrivilegeStatusSearchResponse;

@Repository
public class GalaxyCardPrivilegeStatusRepositoryImpl extends JOOQBaseRepositoryPlus implements GalaxyCardPrivilegeStatusRepository {

    /**
     * GalaxyCardPrivilege查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    public Page<GalaxyCardPrivilegeStatusSearchResponse> list(GalaxyCardPrivilegeStatusSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.fields());
        fields(LOAN_GALAXY_CARD_PRIVILEGE.NAME);
        innerJoin(LOAN_GALAXY_CARD_PRIVILEGE, LOAN_GALAXY_CARD_PRIVILEGE.ID.eq(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.PRIVILEGE_ID));
        eq(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.PRIVILEGE_ID, searchRequest.getPrivilegeId());
        return search(pageable, record -> {
            GalaxyCardPrivilegeStatusSearchResponse response = new GalaxyCardPrivilegeStatusSearchResponse();
            response.setId(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.ID));
            response.setPk(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.ID));
            response.setPrivilegeId(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.PRIVILEGE_ID));
            response.setShow(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.SHOW));
            response.setStatus(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.STATUS));
            response.setAction(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.ACTION));
            response.setTarget(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.TARGET));
            response.setPrivilegeName(record.get(LOAN_GALAXY_CARD_PRIVILEGE.NAME));
            response.setActionString(GalaxyCardPrivilegeServiceImpl.ActionConst.get(response.getAction()).desc());
            return response;
        });
    }

    public Map<String, Long> getPrivilegeList(){
        return dslContext.select(LOAN_GALAXY_CARD_PRIVILEGE.ID, LOAN_GALAXY_CARD_PRIVILEGE.NAME)
                .from(LOAN_GALAXY_CARD_PRIVILEGE)
                .fetchMap(LOAN_GALAXY_CARD_PRIVILEGE.NAME, LOAN_GALAXY_CARD_PRIVILEGE.ID);
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_GALAXY_CARD_PRIVILEGE_STATUS;
    }
}
